import os
import time


def work(file,storepath):
    fd=open(file, encoding='utf-8')
    t1 = time.time()

    lines=fd.readlines()

    #提取 insert xx values 作为第一行
    firstStr = lines[0].split('value')[0] + 'values'
    # print(firstStr)

    #结果string
    batchProcessStr = firstStr

    for line in lines:
        # insert xxx values (xxx);  提取转换成 (xxx),
        str = line.split('values')[1].replace(';', ',')
        batchProcessStr += str

    # 把最后的,号改为;
    batchProcessStr = batchProcessStr[0:-1] + ';'
    # print(batchProcessStr)


    #文件名，不带文件类型
    filename=os.path.basename(file).split(".")[0]
    # win默认桌面路径（未修改桌面路径）
    desktop=os.path.join(os.path.expanduser("~"), 'Desktop')
    if storepath == "":
        print("storepath is '',set storepath is default window desktop")
        storepath = desktop

    # 新文件
    file_write_obj=open(os.path.join(storepath, filename+'_batch.sql'), 'w', encoding="utf-8")
    # 逐行写入
    file_write_obj.write(batchProcessStr)

    print('文件写入成功！')
    file_write_obj.close()

    t2=time.time()
    print("耗时:"+t2-t1)


if __name__=='__main__':
    file='../数据测试集/非批处理转批处理.sql'
    file='C:\\Users\\11578\\Desktop\\837.sql'
    storepath='../数据结果集'
    storepath=''
    work(file,storepath)